import java.util.Calendar;
import java.util.GregorianCalendar;

import org.flat.exceptions.LogicErrorDate;
import org.flatshare.dao.InvoiceDao;
import org.flatshare.entity.Invoice;


public class InvoiceInput {
	
	GregorianCalendar startDate, endDate;
	String id, type;
	float amount;
	InvoiceDao invDao;
	Invoice inv;

    public void createInvoice(String id,String type,float amout, int ggi,int mmi,int aai,int ggf,int mmf,int aaf)throws LogicErrorDate{    		
		boolean dateOk=true;
		startDate=new GregorianCalendar(aai,mmi-1,ggi);
		endDate  =new GregorianCalendar(aaf,mmf-1,ggf);
		
		startDate.setLenient(false);
		endDate.setLenient(false);
					
		try
		{
			startDate.get(Calendar.DATE);
			endDate.get(Calendar.DATE);
		}
		catch(IllegalArgumentException il)
		{
			dateOk=false;
			throw new LogicErrorDate("The dates of the bill are impossible!");
		}

		startDate.setLenient(true);
		endDate.setLenient(true);
		 		
		if(dateOk)
		{
			if(startDate.after(endDate))
			{
				throw new LogicErrorDate("Start Date bill is greater than End Date bill!");
			}
			else
			{
				inv=new Invoice();
				inv.setId(id);
				inv.setType(type);
				inv.setAmount(amout);
				inv.setStartDate(startDate);
				inv.setEndDate(endDate);
				invDao.createInvoice(inv);
			}
		}
	}
    
    public String getId()
	{
		return id;
	}

    public String getType()
	{
		return type;
	}

    public float  getAmount()
	{
		return amount;
	}
	
    public GregorianCalendar getInizioBolletta()
	{
		return startDate;
	}

    public GregorianCalendar getFineBolletta()
	{
		return endDate;
	}
	
    public int getNumDaysInvoice()
	{
		long secStart=startDate.getTimeInMillis();
		long secEnd=endDate.getTimeInMillis();
		long dif=secEnd-secStart;
		return (int) (dif/(1000*60*60*24));
	}
	
    public float getCostDaily()
	{
		return (float)amount/getNumDaysInvoice();
	}			

    public String toString()
	{
		return "The invoice off "+type+" with id "+id+", covers "+getNumDaysInvoice()+" days"+
								" and amounts "+amount+" euro -> "+getCostDaily()+" euro at day.";
	} 
    
}
